package com.example.common.constants;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: Allen
 * @create: 2025/7/10 22:06
 * @description: 错误码统一管理
 **/
public enum Ret {

    //通用
    Success(0, "ok"),
    Error(1, "通用错误"),
    Format_Error(2, "格式错误"),
    Unknown_Implete(3, "格式错误"),
    Server_Error(4, "服务器处理失败"),
    Server_Exception(5, "服务器处理异常，请联系客服"),

    // ========================================
    // 基础系统 100** - 200**
    // ========================================

    //登录
    Account_Logined(10101, "帐号已经登录"),
    Account_UnLogined(10102, "帐号或者角色没有登录"),
    Account_Token_Error(10103, "帐号校验失败"),
    Account_Not_Exist(10104, "帐号不存在"),

    User_Null(10151, "用户不存在"),
    User_Is_Online(10153, "用户已经登录"),
    User_Ban(10154, "用户已被禁"),

    // ========================================
    // 游戏级别功能 21000 - 30000 start
    // ========================================


    // ========================================
    // 运营和活到级别功能 消息号 31000 - 40000 start
    // ========================================
    ;

    /**
     * 正常
     */
    public static final int Ret_Success = 0;

    private final int retCode;
    private final String msg;

    public int getRetCode() {
        return retCode;
    }

    public String getMsg() {
        return msg;
    }

    private Ret(int retCode, String msg) {
        this.retCode = retCode;
        this.msg = msg;
    }

    private static final Map<Integer, Ret> codeMap = new HashMap<>();

    static {
        for (Ret t : values()) {
            if (codeMap.containsKey(t.getRetCode())) {
                throw new Error("duplicate ret code :" + t.getRetCode());
            }
            codeMap.put(t.getRetCode(), t);
        }
    }

    public boolean isSuccess() {
        return retCode == Ret_Success;
    }

    public boolean isError() {
        return retCode != Ret_Success;
    }

    public static Ret convert(int type) {
        return codeMap.get(type);
    }

    public <T> RetResult<T> success(T data) {
        return new RetResult<>(this, data);
    }

    public <T> RetResult<T> error() {
        return new RetResult<>(this);
    }

    @Override
    public String toString() {
        return "{" +
                "\"ret\":" + retCode +
                ", \"msg\":\"" + msg + "\"" +
                '}';
    }
} 